﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SenerPro;

namespace MensUnoShop.Models
{
    public class NormalMarket : Market,IMarket
    {
        #region private attribute
        private string _introduction;       //賣場內容
        private string _standards;          //規格
        private string _washnote;          //洗滌注意
        private string _shoppingnote;          //消費注意
        private List<MarketImage> _marketimg;
        private DBMarket _marketrepository;
        private IMarketSalesKit _marketsaleskit;
        #endregion

        public string INTRODUCTION { set { _introduction = value; } get { return _introduction; } }
        public string STANDARDS { set { _standards = value; } get { return _standards; } }
        public string WASHNOTE { set { _washnote = value; } get { return _washnote; } }
        public string SHOPPINGNOTE { set { _shoppingnote = value; } get { return _shoppingnote; } }
        public List<MarketImage> MARKETIMG { set { _marketimg = value; } get { return _marketimg; } }
        public IMarketSalesKit MARKETSALESKIT { set { _marketsaleskit = value; } get { return _marketsaleskit; } }
    

        #region public method

        public NormalMarket() { _marketrepository = new DBMarket(); } 

        
        /// <summary>
        /// 取得該賣場的折扣saleskit
        /// </summary>
        public void GetMarketSalesKit(int buyamount) 
        {
            int total = buyamount * _price;
            _marketsaleskit = _marketrepository.GetMarketSalesKit(_mkguid, buyamount, total);
        }
        /*
        /// <summary>
        /// 取得加購賣場清單
        /// </summary>
        /// <returns></returns>
        public List<SimpleMarket> GetAddtionMarketList() { return null; }

        /// <summary>
        /// 取得同類賣場清單
        /// </summary>
        /// <returns></returns>
        public List<SimpleMarket> GetSameMarketList() { return null; }


        public void SetRepository(IMarketRepository repository)
        {
            if (_marketrepository == null)
                _marketrepository = repository;
        }
        */



        /* 購物車
        public bool CheckMarket(int subcatalogid, int catalogid, Guid scguid, ref List<OrderMarket> mlist, int[] productidlist, int[] specidlist, int[] amountlist, ref string error)
        {
            int oriamount = 0;
            bool ischeck = false;
            bool isexist = false;
            //確認該賣場規格數量是否OK
            var mm = mlist.SingleOrDefault(m => m.MKID==_mid && m.OPLIST[0].SPECID == specidlist[0]);
            if (mm != null && mm.OPLIST[0].AMOUNT + amountlist[0] > mm.OPLIST[0].MAXBUY)
                error = "本賣場已達購買數量上限!!";
            else
            {
                if (DateTime.Compare(_sdate, DateTime.Now) < 0 && DateTime.Compare(_edate, DateTime.Now) > 0 && _ispublish)
                {
                    _subcatalogid = subcatalogid;
                    if (mm != null)
                    {
                        isexist = true;
                        oriamount = mm.OPLIST[0].AMOUNT;
                        amountlist[0] += mm.OPLIST[0].AMOUNT;
                        mm.OPLIST[0].AMOUNT = amountlist[0];
                    }
                    else
                    {
                        mm = initialOrderMarket(productidlist,amountlist,specidlist);
                        mm.CATALOGID = catalogid == 0 ? _catalogid.FirstOrDefault() : catalogid;
                        mlist.Add(mm);
                    }
                    if (_marketrepository.SetSpecStock(scguid, mm.MOKGUID, mm.MKID, mm.MKTYPE, specidlist[0], amountlist[0]))
                    {
                        mm.PRICE = mm.OPLIST[0].PRICE * amountlist[0];
                        //一般賣場的數量=商品的數量
                        mm.AMOUNT = mm.OPLIST[0].AMOUNT;
                        GetMarketSalesKit(amountlist[0]);
                        if (_marketsaleskit != null && _marketsaleskit.TYPE == SalesKitType.PriceDiscount)
                        {
                            ITradeInDiscount dsk = (ITradeInDiscount)_marketsaleskit;
                            dsk.GetDiscount();
                            mm.DISCOUNT = dsk.DEPRICE;
                            mm.DISCOUNTNOTE = _marketsaleskit.NOTE;
                        }

                        ischeck = true;
                    }
                    else
                    {
                        error = "本賣場暫無庫存!!";
                        //數量還原或移除新增的購物賣場
                        if (isexist)
                        {
                            mm.OPLIST[0].AMOUNT = oriamount;
                        }
                        else
                            mlist.Remove(mm);
                    }
                }
                else
                {
                    error = "本賣場尚未開賣!!";
                }
                
            }
            return ischeck;
        }

        public bool SetMarketSpec(ref OrderMarket om, Guid scguid, int specid)
        {
            bool ischk = false;
            GetProductSpec();
            try
            {
                var spec = _plist[0].SPEC.SingleOrDefault(sp => sp.SID == specid);
                if (spec != null)
                {
                    if (_marketrepository.SetSpecStock(scguid, om.MOKGUID, om.MKID, om.MKTYPE, specid, 1))
                    {
                        if (_marketrepository.SetSpecStock(scguid, om.MOKGUID, om.MKID, om.MKTYPE, om.OPLIST[0].SPECID, 0))
                        {
                            om.OPLIST[0].SPECID = spec.SID;
                            om.OPLIST[0].SPECTITLE = spec.TITLE;
                            om.OPLIST[0].AMOUNT = 1;
                            om.OPLIST[0].MAXBUY = _maxbuy;
                            om.PRICE = om.OPLIST[0].PRICE * 1;
                            om.AMOUNT = 1;
                            GetMarketSalesKit(1);
                            if (_marketsaleskit != null && _marketsaleskit.TYPE == SalesKitType.PriceDiscount)
                            {
                                ITradeInDiscount dsk = (ITradeInDiscount)_marketsaleskit;
                                dsk.GetDiscount();
                                om.DISCOUNT = dsk.DEPRICE;
                                om.DISCOUNTNOTE = _marketsaleskit.NOTE;
                            }
                            ischk = true;
                        }
                        else
                            _marketrepository.SetSpecStock(scguid, om.MOKGUID, om.MKID, om.MKTYPE, specid, 0);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger log = new Logger(Tools.logpath);
                log.RecordException(log.FlowLogPath, "[NormalSetSpecStock]" + scguid + "\r\n" + ex.ToString());
            }
            return ischk;
        }

        public bool SetMarketAmount(ref OrderMarket om, Guid scguid, int itemid, int amount)
        {
            bool ischk = false;
            try
            {
                if (_marketrepository.SetSpecStock(scguid, om.MOKGUID, om.MKID, om.MKTYPE, om.OPLIST[0].SPECID, amount))
                {
                    om.OPLIST[0].AMOUNT = amount;
                    om.PRICE = om.OPLIST[0].PRICE * amount;
                    om.AMOUNT = om.OPLIST[0].AMOUNT;
                    GetMarketSalesKit(amount);
                    if (_marketsaleskit != null && _marketsaleskit.TYPE == SalesKitType.PriceDiscount)
                    {
                        ITradeInDiscount dsk = (ITradeInDiscount)_marketsaleskit;
                        dsk.GetDiscount();
                        om.DISCOUNT = dsk.DEPRICE;
                        om.DISCOUNTNOTE = _marketsaleskit.NOTE;
                    }
                    ischk = true;
                }
            }
            catch (Exception ex)
            {
                Logger log = new Logger(Tools.logpath);
                log.RecordException(log.FlowLogPath, "[NormalSetAmount]" + scguid + "\r\n" + ex.ToString());
            }
            return ischk;
        }
         * */
        #endregion

        public bool SetMarketAmount(ref OrderMarket om, Guid scguid, int num)
        {
            bool ischk = false;
            try
            {
                if (_marketrepository.SetSpecStock(scguid, om.MOKGUID,om.MKGUID, om.SPECID, num))
                {
                    om.AMOUNT = num;
                    om.PRICE = om.SINGLEPRICE * num;
                    GetMarketSalesKit(num);
                    if (_marketsaleskit != null && _marketsaleskit.TYPE == Config.SalesKitType.PriceDiscount)
                    {
                        ITradeInDiscount dsk = (ITradeInDiscount)_marketsaleskit;
                        dsk.GetDiscount();
                        om.DISCOUNT = dsk.DEPRICE;
                        om.DISCOUNTNOTE = _marketsaleskit.NOTE;
                    }
                    ischk = true;
                }
            }
            catch (Exception ex)
            {
                Logger log = new Logger(Config.logpath);
                log.RecordExceptionFormat(log.FlowLogPath, "[NormalSetAmount]",scguid + "\r\n" + ex.ToString());
            }
            return ischk;
        }

        public bool CheckMarket(Guid scguid, ref List<OrderMarket> mlist, Guid[] pdguid, string[] specid, int[] amount, ref string error)
        {
            int oriamount = 0;
            bool ischeck = false;
            bool isexist = false;
            Guid _pdguid = pdguid[0];
            Guid _specid = new Guid(specid[0]);
            int _amount = amount[0];
            //確認該賣場規格數量是否OK
            var mm = mlist.SingleOrDefault(m => m.MKGUID == _mkguid && m.SPECID == _specid && m.TYPE != Config.MarketType.Combo);
                if (DateTime.Compare(_sdate, DateTime.Now) < 0 && DateTime.Compare(_edate, DateTime.Now) > 0 && _status)
                {
                    if (mm != null && mm.TYPE != Config.MarketType.Combo)
                    {
                        isexist = true;
                        oriamount = mm.AMOUNT;
                        _amount += mm.AMOUNT;
                        mm.AMOUNT = _amount;
                    }
                    else
                    {
                        mm = initialOrderMarket(_pdguid, _amount, _specid);
                        mlist.Add(mm);
                    }
                    if (_marketrepository.SetSpecStock(scguid, mm.MOKGUID, mm.MKGUID, _specid, _amount))
                    {
                        mm.PRICE = mm.SINGLEPRICE * _amount;
                        //一般賣場的數量=商品的數量
                        GetMarketSalesKit(_amount);
                        if (_marketsaleskit != null && _marketsaleskit.TYPE == Config.SalesKitType.PriceDiscount)
                        {
                            ITradeInDiscount dsk = (ITradeInDiscount)_marketsaleskit;
                            dsk.GetDiscount();
                            mm.DISCOUNT = dsk.DEPRICE;
                            mm.DISCOUNTNOTE = _marketsaleskit.NOTE;
                        }

                        ischeck = true;
                    }
                    else
                    {
                        error = "本賣場暫無庫存!!(" + mm.MKGUID + _specid + ")";
                        //數量還原或移除新增的購物賣場
                        if (isexist)
                        {
                            mm.AMOUNT = oriamount;
                        }
                        else
                            mlist.Remove(mm);
                        //合購商品要把同ID的移除
                        if(mm.TYPE==Config.MarketType.Combo)
                        {
                            var combolist = mlist.Where(m=>m.TYPE==Config.MarketType.Combo && m.COMBOID==mm.COMBOID);
                            foreach (var mt in combolist)
                                _marketrepository.FreeSpecStock(scguid, mt.MOKGUID);
                        }
                    }
                }
                else
                {
                    error = "本賣場尚未開賣!!";
                }


            return ischeck;
        }
    }
}
